var App = getApp();
Page({
  data: {},
  onLoad (param) {
    let userId = wx.getStorageSync('userId')
    this.setData({ userId: userId })
    this.onPullDownRefresh()
  },
  userInfo () { // 获取积分
    let { userId } = this.data
    App.HttpService.userInfo({userId: userId}).then(res => {
      let data = res.data
      if (data.code == 200) {
        this.setData({detail: data.data})
      }
    })
  },
  initData() {
    let { userId } = this.data
    this.setData({
      order: {
        items: [],
        total: '',
        params: {
          buyerId: userId,
          page: 1,
          pageCount: 10
        }
      }
    })
  },
  getList (cb) { // 积分流水
    let order = this.data.order
    let params = order.params
    App.HttpService.intBill(params).then(res => {
      cb && cb()
      let data = res.data
      if (data.code == 200) {
        order.items = [...order.items, ...data.data.list]
        if (data.data.list.length > 0) {
          params.page = params.page + 1
          order.total = data.data.counts
        }
        this.setData({
          order: order
        })
      }
    }, e => {
      cb && cb()
    })
  },
  onPullDownRefresh () {
    wx.showNavigationBarLoading()
    this.userInfo()
    this.initData()
    this.getList(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    })
  },
  onReachBottom () {
    let order = this.data.order
    if (order.items.length === order.total) {
      return
    }
    this.getList()
  },
  toOrder () { // 积分订单
    wx.navigateTo({
      url: '/pages/integral/list/index'
    })
  },
})